<?php
namespace JsonRpc\Services\System;
use JsonRpc\Models\SysButton;
use JsonRpc\Models\SysMenu;
use JsonRpc\Models\SysMenuDataRule;
use JsonRpc\Models\SysMenuOperation;
use JsonRpc\Services\CommonService;
use JsonRpc\Utils\Utils;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class MenuService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\SysMenu";
    }

    public static function ping(){
        return 'OK';
    }

    //获取菜单树表
    public static function listTreeGridData($params)
    {
        if(isset($params['sortby'])){
            $params['sortby'] = array_unshift($params['sortby'], "sort", "id");
        }else{
            $params['sortby'][] = 'sort';
            $params['sortby'][] = 'id';
        }

        $menulist = self::getInfoList($params);
        $menu_tree = Utils::make_treeTable($menulist, 'id', 'parent_id', 'children');
        return $menu_tree;
    }

    //获取菜单树
    public static function listTreeData($params)
    {
        if(isset($params['sortby'])){
            $params['sortby'] = array_unshift($params['sortby'], "sort", "id");
        }else{
            $params['sortby'] = ['sort','id'];
        }

        $params['select'] = ['id','name','parent_id'];

        $menulist = self::getInfoList($params);

        if(!isset($params['checked_id'])){
            $params['checked_id'] = 0;
       }
        $menu_tree = Utils::make_tree($menulist, 'id', 'parent_id', 'name', 0, true, $params['checked_id']);
        return $menu_tree;
    }

    public static function saveData($params){
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysMenu::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SysMenu();
            $model->client_id = 0;
            $model->org_id = 0;
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }

        if($params['parent_id']){
            $parent = SysMenu::find($params['parent_id']);
            $model->menu_level = $parent['menu_level']+1;
        }else{
            $model->menu_level = 1;
        }

        $model->code = $params['code'];
        $model->name = $params['name'];
        $model->parent_id = $params['parent_id'];
        $model->icon_style = $params['icon_style'];
        $model->menu_url = $params['menu_url'];
        $model->permission = $params['permission'];
        $model->sort = $params['sort'];
        return $model->save();
    }

    /**
     * 获取菜单的按钮信息
     * @param $params
     */
    public function getMenuBtn($params){
        $menu_id = $params['menu_id'];
        $info  = SysButton::get()->toArray();

        if(!empty($info)){
              foreach ( $info as $key=>$item){
                   $is_set = SysMenuOperation::where('menu_id',$menu_id)->where('button_id',$item['id'])->first();
                   if($is_set){
                       $info[$key]['checked'] = true;
                   }else{
                       $info[$key]['checked'] = false;
                   }
              }
        }
        return $info;
    }

    /**
     * 保存菜单按钮
     * @param $params
     * @return bool
     */
    public function saveBtn($params){
        $menu_id = $params['menu_id'];
        $ids = $params['ids'];
        //首先删除当前首要的menu_id
        SysMenuOperation::where('menu_id',$menu_id)->delete();
        $button_ids = explode(',',$ids);
        foreach ($button_ids as $item){
            $model = new SysMenuOperation();
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            $model->client_id = 0;
            $model->org_id = 0;
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
            $model->is_active = 1;
            $model->menu_id = $menu_id;
            $model->button_id = 1;
            $model->button_id = $item;
            $model->save();
        }
        return true;
    }

    /**
     * 获取菜单数据权限
     * @param $params
     * @return array
     */
    public function getMenuDatalist($params){
        $menu_id = $params['menu_id'];

        $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
        $MenuDataRule = SysMenuDataRule::where('client_id', $user['client_id'])
            ->where('menu_id',$menu_id)
            ->get();
        if(!empty($MenuDataRule)){
            return $MenuDataRule->toArray();
        }
        return [];
    }

    public function saveMenuRule($params){
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysMenuDataRule::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SysMenuDataRule();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->name = $params['name'];
        $model->rule = $params['rule'];
        $model->is_active = $params['is_active'];
        $model->menu_id = $params['menu_id'];
        return $model->save();
    }

    public function getMenuRuleById($params){
       $data =  SysMenuDataRule::find($params['id']);
       if($data){
            return $data->toArray();
       }
       return [];
    }
}
